package com.sunjob.mroassets.server.serverImpl;

import cn.dev33.satoken.stp.StpUtil;
import com.sunjob.common.domain.assets.Accessory;
import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import com.sunjob.common.domain.mroresume.WHBus;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroassets.mapper.InstanceMapper;
import com.sunjob.mroassets.server.InstanceService;
import com.sunjob.mroassets.server.otherServer.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class InstanceServiceImpl implements InstanceService {
    @Autowired
    InstanceMapper instanceMapper;
    @Autowired
    Resume resume;
    @Override
    //向数据库存入实例信息
    public void insertInstance(Instance instance1) {
        //获取id、创建时间、修改时间、创建人、修改人，将其存入实例对象中
        instance1.setInstanceId(MROUtils.nextId());
        instance1.setCreateDate(LocalDateTime.now());
        instance1.setModifyDate(LocalDateTime.now());
        instance1.setCreator((Long) StpUtil.getLoginId());
        instance1.setModifier((Long) StpUtil.getLoginId());
        instance1.setStatus(11);
        instanceMapper.insertInstance(instance1);
        for (Part part : instance1.getParts()) {
            //循环遍历实例下的部件
            //获取id、实例id、创建时间、修改时间、创建人、修改人，将其存入部件对象中
            part.setPartId(MROUtils.nextId());
            part.setInstanceId(instance1.getInstanceId());
            part.setCreateDate(LocalDateTime.now());
            part.setModifyDate(LocalDateTime.now());
            part.setCreator((Long) StpUtil.getLoginId());
            part.setModifier((Long) StpUtil.getLoginId());
            part.setStatus(21);
            int jtime = 0;
            for (Accessory accessory : part.getAccessorys()) {
                //循环遍历部件下的零件
                //获取id、部件id、创建时间、修改时间、创建人、修改人，将其存入零件对象中
                accessory.setAccessoryId(MROUtils.nextId());
                accessory.setPartId(part.getPartId());
                accessory.setCreateDate(LocalDateTime.now());
                accessory.setModifyDate(LocalDateTime.now());
                accessory.setCreator((Long) StpUtil.getLoginId());
                accessory.setModifier((Long) StpUtil.getLoginId());
                accessory.setStatus(31);
                if (accessory.getTime()>jtime){
                    jtime = accessory.getTime();
                }
                //保存零件信息
                instanceMapper.insertAccessory(accessory);
            }
            part.setTime(jtime);
            part.setLastTime(jtime);
            //保存部件信息
            instanceMapper.insertPart(part);
        }
        //调用履历管理接口进行信息录入
        WHBus whBus = new WHBus();
        whBus.setInstance_id(instance1.getInstanceId());
        whBus.setInstance_name(instance1.getInstanceName());
        whBus.setInstance_brand(instance1.getInstanceBrand());
        whBus.setInstance_mileage(instance1.getInstanceMileage());
        whBus.setInstance_vender(instance1.getInstanceVender());
        whBus.setCreate_date(instance1.getCreateDate());
        whBus.setTime(instance1.getTime());
        resume.insertBus(whBus);
        System.out.println("------远程调用履历管理接口完成------");
    }

    @Override
    //从数据库中获取实例信息
    public Object queryEntityByTypeAndID(Long entityId, String type) {
        //判断实体类型
        if ("instance".equals(type)){
            //若为实例类型则通过id查找信息
            Instance instance = instanceMapper.queryInstanceById(entityId);
            //获取实例的部件信息
            instance.setParts(instanceMapper.queryPartsByInstanceId(instance.getInstanceId()));
            for (Part part : instance.getParts()) {
                //获取部件的零件信息
                part.setAccessorys(instanceMapper.queryAccessoryByPartId(part.getPartId()));
            }
            return instance;
        }else if ("part".equals(type)){
            //若为部件类型，则通过部件获取实例主体信息
            Instance instance = instanceMapper.queryInstanceByPartId(entityId);
            //获取实例的部件信息
            instance.setParts(instanceMapper.queryPartsByInstanceId(instance.getInstanceId()));
            for (Part part : instance.getParts()) {
                //获取部件的零件信息
                part.setAccessorys(instanceMapper.queryAccessoryByPartId(part.getPartId()));
            }
            return instance;
        }
        //若输入的实体类型有误，则返回空值
        return null;
    }

    @Override
    //仅批量插入部件信息
    public int insertParts(List<Part> parts) {
        for (Part part : parts) {
            //获取id、创建时间、修改时间、创建人、修改人，将其存入部件对象中
            part.setPartId(MROUtils.nextId());
            part.setCreateDate(LocalDateTime.now());
            part.setModifyDate(LocalDateTime.now());
            part.setCreator((Long) StpUtil.getLoginId());
            part.setModifier((Long) StpUtil.getLoginId());
            part.setStatus(21);
            //保存部件信息
            instanceMapper.insertPart(part);
        }
        return 0;
    }

    @Override
    public List<Instance> getAllInstance() {
        return instanceMapper.getAllInstance();
    }

    @Override
    public List<Part> getAllPart() {
        return instanceMapper.getAllPart();
    }

    @Override
    public List<Instance> getInstanceByShapeId(Long shapeId) {
        return instanceMapper.getInstanceByShapeId(shapeId);
    }
}
